Beginning Unit Testing Tutorial in C# with NUnit (Part 1)
So how do you get started with Unit Testing? Well, you need a Unit Testing framework and some basic know how. This article will help you with both.
NUnit is a commonly used testing framework for C#. Well, NUnit has a guide but I found it incomplete for a newbie, though perfectly acceptable for an experienced developer.
http://nunit.org/index.php?p=quickStart&r=2.6
Lets assume that you have a project with this class:
namespace NUnitBasics { public class SimpleAddSubtractTool { public int Add(int value1, int value2) { return value1 + value2; } public int Subtract(int value1, int value2) { return value1 - value2; } } }
You need to test these methods and find bugs in them and despite how simple they appear, there are bugs in the above methods.
Step 1 – Install NUnit
- Go to this url and download the latest version of NUnit.
http://nunit.org/?p=download - Run the installer.
Step 2 – Create an NUnit testing project in Visual Studio
You can do this manually, as described below, or you can download my templates: NUnit Project Template for Visual Studio
Part 1 – Creating the Project
- In Visual Studio, open the solution that holds the project and code that needs to be tested.
- Right-click on the solution and choose Add | New Project.
- In the tree on the left, select Visual C# | Windows.
Note: You would think you would select a Test project, but that is for the testing framework built into Visual Studio, MSTest. - Click to highlight Class Library.
- Provide a project name.
Important: It is usually best practice to give the test project the same name as the project it is test only with the word “test” at the end. For example, a project called MyProject would have a corresponding test project called MyProjectTests. - Click Ok.
Part 2 – Configuring the project
- Click Reference | Add Reference.
- Click the first tab: .NET
- Find and add nunit.framework and nunit.mocks.
- Click Reference | Add Reference (again).
- Choose Projects.
- Select the project that is being tested.
- Click Ok.
- Delete the Class1.cs as it is not used.
Step 3 – Start your first Unit Test
You can add a class and manually change it to be an NUnit test class as described below, or you can download my template: NUnit Item Template for Visual Studio
Part 1 – Create the class file
- Right-click on your project and choose Add | Class.
- Name the class after the class you are going to test. For example, a class called SimpleAddSubtractTool would have a corresponding test class called SimpleAddSubtractToolTests.
- Click OK.
Part 2 – Convert to an NUnit class file
- Add a using reference to NUnit.Framwork.
- Add the [TestFixture] attribute above the class name.
- Add Setup and Tear down attributes and methods if needed.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using NUnit.Framework; namespace NUnitBasicsTests { /// <summary> /// A test class for ... /// </summary> [TestFixture] public class SampleTest { #region Setup and Tear down /// <summary> /// This runs only once at the beginning of all tests and is used for all tests in the /// class. /// </summary> [TestFixtureSetUp] public void InitialSetup() { } /// <summary> /// This runs only once at the end of all tests and is used for all tests in the class. /// </summary> [TestFixtureTearDown] public void FinalTearDown() { } /// <summary> /// This setup funcitons runs before each test method /// </summary> [SetUp] public void SetupForEachTest() { } /// <summary> /// This setup funcitons runs after each test method /// </summary> [TearDown] public void TearDownForEachTest() { } #endregion } }
You are now ready to create your first test.
Note: For this basic class you can actually delete the Setup and Tear down region as it won’t be used, however, it is good to know how to use it when you get started.
Beginning Unit Testing Tutorial in C# with NUnit (Part 2)
Return to C# Unit Test Tutorial
[…] For Nunit Fundamentals, you can refer this link. […]
Thanks! This was very helpful!